﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using KenLib.Common;
using XBLib.Dal.AdoDal;
//using System.Web.SessionState;

/// <summary>
/// Summary description for CommonFunction
/// </summary>
public class CommonFunction : System.Web.UI.Page
{
    const string ASCENDING = " ASC";
    const string DESCENDING = " DESC";
    string strErr;
    #region SORT
    private SortDirection GridViewSortDirection
    {
        get
        {
            if (ViewState["sortDirection"] == null)
                ViewState["sortDirection"] = SortDirection.Ascending;
            return (SortDirection)ViewState["sortDirection"];
        }
        set { ViewState["sortDirection"] = value; }
    }
    void AddSortImage(int columnIndex, GridViewRow headerRow, string[] Texts, string[] Sorts)
    {
        //string[] Texts = { "氏名", "性別", "国籍", "生年月日", "会社携帯" };
        //string[] Sorts = { "氏名", "性別", "国籍", "生年月日", "会社携帯" };
        // Create the sorting image based on the sort direction.
        LinkButton lblSort = new LinkButton();
        lblSort.ID = Sorts[columnIndex];
        if (GridViewSortDirection == SortDirection.Ascending)
        {
            lblSort.Text = Texts[columnIndex] + "▲";
            lblSort.ToolTip = "Ascending Order";
        }
        else
        {
            lblSort.Text = Texts[columnIndex] + "▼";
            lblSort.ToolTip = "Descending Order";
        }
        lblSort.CssClass = "aSort";
        lblSort.Click += new EventHandler(lblSortClick);
        // Add the image to the appropriate header cell.

        headerRow.Cells[columnIndex].Text = "";
        headerRow.Cells[columnIndex].Controls.Add(lblSort);
    }
    public  virtual void LoadData(string strSortExpression, string strSortDirection) { }
    void lblSortClick(object sender, EventArgs e)
    {
        LinkButton lblSort = sender as LinkButton;
        string sortExpression = lblSort.ID;
        GridViewSortDirection = GridViewSortDirection == SortDirection.Ascending ? SortDirection.Descending : SortDirection.Ascending;
        LoadData(sortExpression, GridViewSortDirection == SortDirection.Ascending ? ASCENDING : DESCENDING);
    }
    int GetSortColumnIndex(GridView grv)
    {
        foreach (DataControlField field in grv.Columns)
        {
            if (field.SortExpression == (string)ViewState["SortExpression"])
            {
                return grv.Columns.IndexOf(field);
            }
        }
        return -1;
    }
    #endregion
    #region EVENT SORT
    public void grv_RowCommand(GridView grv, GridViewCommandEventArgs e, string hd_pageCountValue)
    {
        //if (e.CommandName == "view")
        //{
        //    Response.Redirect("frm_HumanBasicInfo.aspx?id=" + e.CommandArgument.ToString() + "&act=u");
        //    // Response.Redirect("frm_HumanEnterInfo.aspx?id=22&act=u");
        //}
        //else 
        if (e.CommandName == "Page")
        {
            int pageCount = Convert.ToInt32(hd_pageCountValue);
            int intCurIndex = grv.PageIndex;
            switch (e.CommandArgument.ToString().ToLower())
            {
                case "first":
                    grv.PageIndex = 0;

                    break;
                case "prev":
                    if (intCurIndex > 0)
                        grv.PageIndex = intCurIndex - 1;
                    break;
                case "next":
                    if (intCurIndex < pageCount - 1)
                        grv.PageIndex = intCurIndex + 1;
                    break;
                case "last":
                    grv.PageIndex = pageCount - 1;
                    break;
            }

            LoadData((string)ViewState["SortExpression"], GridViewSortDirection == SortDirection.Ascending ? ASCENDING : DESCENDING);
            GridViewRow gvrPager = grv.FooterRow;
            if (gvrPager == null) return;
            DropDownList ddlPages = (DropDownList)gvrPager.Cells[0].FindControl("ddlPages");
            ddlPages.SelectedIndex = grv.PageIndex;
        }
    }
    public void ddlPages(GridView grv)
    {
        try
        {
            GridViewRow gvrPager = grv.FooterRow;
            DropDownList ddlPages = (DropDownList)gvrPager.Cells[0].FindControl("ddlPages");
            grv.PageIndex = ddlPages.SelectedIndex;

            LoadData((string)ViewState["SortExpression"], GridViewSortDirection == SortDirection.Ascending ? ASCENDING : DESCENDING);
        }
        catch (LocalException ex)
        {
            strErr = ex.Message;
        }
        catch (Exception ex)
        {
            ExportFile.WriteErr(Session["id"] as string, Session["sessionManager"] as string, "", ex.Message, DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"), DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"));
            strErr = ex.Message;
        }
        //finally
        //{
        //    if (strErr != "")
        //        Response.Redirect(ConfigurationSettings.AppSettings["PAGE_ERROR"] + "?err=" + Server.UrlEncode(strErr), false);
        //}
    }
    public void grv_DataBound(GridView grv, int count)
    {
        try
        {
            GridViewRow gvrPager = grv.FooterRow;
            if (gvrPager == null) return;
            DropDownList ddlPages = (DropDownList)gvrPager.Cells[0].FindControl("ddlPages");
            Label lblPageCount = (Label)gvrPager.Cells[0].FindControl("lblPageCount");
            if (ddlPages != null)
            {
                for (int i = 0; i < count; i++)
                {
                    int intPageNumber = i + 1;
                    ListItem lstItem = new ListItem(intPageNumber.ToString());
                    if (i == grv.PageIndex)
                        lstItem.Selected = true;
                    ddlPages.Items.Add(lstItem);
                }
            }

            if (lblPageCount != null)
                lblPageCount.Text = count.ToString();

        }
        catch (LocalException ex)
        {
            strErr = ex.Message;
        }
        catch (Exception ex)
        {
            ExportFile.WriteErr(Session["id"] as string, Session["sessionManager"] as string, "", ex.Message, DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"), DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"));
            strErr = ex.Message;
        }
        //finally
        //{
        //    if (strErr != "")
        //        Response.Redirect(ConfigurationSettings.AppSettings["PAGE_ERROR"] + "?err=" + Server.UrlEncode(strErr), false);
        //}
    }
    public void grv_RowCreated(GridView grv, GridViewRowEventArgs e, string[] Texts, string[] Sorts)
    {
        try
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                int sortColumnIndex = GetSortColumnIndex(grv);
                if (sortColumnIndex >= 0)
                {
                    AddSortImage(sortColumnIndex, e.Row, Texts, Sorts);
                }

            }
            if (e.Row.RowType == DataControlRowType.Footer)
            {

                for (int i = 0; i < e.Row.Cells.Count; i++)
                {
                    if (i == 0)
                        e.Row.Cells[i].ColumnSpan = e.Row.Cells.Count;
                    else
                        e.Row.Cells[i].Visible = false;

                }
            }
        }
        catch (LocalException ex)
        {
            strErr = ex.Message;
        }
        catch (Exception ex)
        {
            ExportFile.WriteErr(Session["id"] as string, Session["sessionManager"] as string, "", ex.Message, DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"), DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"));
            strErr = ex.Message;
        }
        //finally
        //{
        //    if (strErr != "")
        //        Response.Redirect(ConfigurationSettings.AppSettings["PAGE_ERROR"] + "?err=" + Server.UrlEncode(strErr), false);
        //}
    }

    public void grv_Sorting(object sender, GridViewSortEventArgs e)
    {
        try
        {
            string sortExpression = e.SortExpression;
            ViewState["SortExpression"] = sortExpression;

            if (GridViewSortDirection == SortDirection.Ascending)
            {
                GridViewSortDirection = SortDirection.Descending;
                LoadData(sortExpression, DESCENDING);
            }
            else
            {
                GridViewSortDirection = SortDirection.Ascending;
                LoadData(sortExpression, ASCENDING);
            }
        }
        catch (LocalException ex)
        {
            strErr = ex.Message;
        }
        catch (Exception ex)
        {
            ExportFile.WriteErr(Session["id"] as string, Session["sessionManager"] as string, "", ex.Message, DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"), DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"));
            strErr = ex.Message;
        }
        //finally
        //{
        //    if (strErr != "")
        //        Response.Redirect(ConfigurationSettings.AppSettings["PAGE_ERROR"] + "?err=" + Server.UrlEncode(strErr), false);
        //}
    }
    #endregion
}
